/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.madpie.ui;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.madpie.schemas.ComponentClassType;
import org.madpie.schemas.ComponentType;
import org.madpie.schemas.Machine;
import org.madpie.schemas.Mapping;
import org.madpie.schemas.PhysicalType;
import org.madpie.schemas.VirtualType;
import org.madpie.util.SchemaUtils;

/**
 *
 * @author Niklas Rehfeld
 */
public class HardwareMappingPanel extends javax.swing.JPanel
{

    Map<String, String> videoCards;
    Map<String, String> networkCards;
    Machine mach;
    File out;

    /**
     * Creates new form HardwareMappingPanel
     */
    public HardwareMappingPanel()
    {
        initComponents();
        videoCards = new HashMap<String, String>();
        networkCards = new HashMap<String, String>();
        populateCommandMappings();
        networkCombo.setModel(new DefaultComboBoxModel(networkCards.keySet().
                toArray()));
        videoCombo.setModel(new DefaultComboBoxModel(videoCards.keySet().
                toArray()));
    }

    public HardwareMappingPanel(File f)
    {
        out = f;
        initComponents();
        videoCards = new HashMap<String, String>();
        networkCards = new HashMap<String, String>();
        populateCommandMappings();
        networkCombo.setModel(new DefaultComboBoxModel(networkCards.keySet().
                toArray()));
        videoCombo.setModel(new DefaultComboBoxModel(videoCards.keySet().
                toArray()));
    }
    
    protected void setOutputFile(File f)
    {
        out = f;
    }

    private void populateCommandMappings()
    {
        videoCards.put("Standard VGA", "std");
        videoCards.put("Cirrus CL-GD5446", "cirrus");
        videoCards.put("VMWare", "vmware");
        networkCards.put("Intel i82550 Ethernet", "i82550");
        networkCards.put("Intel i82551 Ethernet", "i82551");
        networkCards.put("Intel i82557A Ethernet", "i82557a");
        networkCards.put("Intel Gigabit Ethernet", "e1000");
        networkCards.put("VirtIO Network", "virtio-net-pci");
    }

    public void populateDiscovered(Machine m)
    {
        mach = m;
        for (ComponentType c : mach.getComponent())
        {
            if (c.getClazz() == ComponentClassType.NETWORK)
            {
                networkText.setText("Network: " + c.getVendor() + " - " + c.
                        getName());
            }
            else if (c.getClazz() == ComponentClassType.GRAPHICS)
            {
                videoText.setText("Video: " + c.getVendor() + " - " + c.
                        getName());
            }
        }
    }

    public void getDiscoveredHwFromFile(File f)
    {
        try
        {
            Machine m = SchemaUtils.ReadHardwareFile(f);
            populateDiscovered(m);
        } catch (FileNotFoundException ex)
        {
            Logger.getLogger(HardwareMappingPanel.class.getName()).
                    log(Level.SEVERE, null, ex);
        } catch (IOException ex)
        {
            Logger.getLogger(HardwareMappingPanel.class.getName()).
                    log(Level.SEVERE, null, ex);
        }
    }

    protected void writeOutMappings(File f) throws JAXBException
    {
        Mapping m = new Mapping();
        m.setArchitecture(mach.getProcessor().getArchitecture());
        List<PhysicalType> phys = m.getPhysical();
        PhysicalType pt = new PhysicalType();
        pt.setName(videoText.getText());
        pt.setMapsTo((String) videoCombo.getSelectedItem());
        phys.add(pt);
        VirtualType vt = new VirtualType();
        vt.setName(videoCombo.getSelectedItem().toString());
        vt.setCommandString(videoCards.
                get((String) videoCombo.getSelectedItem()));
        m.getVirtual().add(vt);

        pt = new PhysicalType();
        pt.setName(networkText.getText());
        pt.setMapsTo((String) networkCombo.getSelectedItem());
        phys.add(pt);

        vt = new VirtualType();
        vt.setName(networkCombo.getSelectedItem().toString());
        vt.setCommandString(networkCards.get((String) networkCombo.
                getSelectedItem()));
        m.getVirtual().add(vt);

        JAXBContext jcb = JAXBContext.newInstance(Mapping.class);
        Marshaller marsh = jcb.createMarshaller();
        marsh.marshal(m, f);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jTextField1 = new javax.swing.JTextField();
        videoLabel = new javax.swing.JLabel();
        videoCombo = new javax.swing.JComboBox();
        networkLabel = new javax.swing.JLabel();
        networkCombo = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        videoText = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        networkText = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        saveMappingButton = new javax.swing.JButton();

        jTextField1.setText("jTextField1");

        videoLabel.setText("Video: ");

        videoCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        networkLabel.setText("Network");

        networkCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        videoText.setEditable(false);
        videoText.setColumns(20);
        videoText.setRows(1);
        jScrollPane1.setViewportView(videoText);

        networkText.setEditable(false);
        networkText.setColumns(20);
        networkText.setRows(1);
        jScrollPane2.setViewportView(networkText);

        jLabel1.setText("Map To:");

        jLabel2.setText("Map To:");

        saveMappingButton.setText("Save");
        saveMappingButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                saveMappingButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(videoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(24, 24, 24)
                        .addComponent(videoCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(24, 24, 24))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(networkLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(networkCombo, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(saveMappingButton)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 480, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(videoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(videoCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(networkLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(networkCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addComponent(saveMappingButton)
                .addContainerGap(85, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void saveMappingButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_saveMappingButtonActionPerformed
    {//GEN-HEADEREND:event_saveMappingButtonActionPerformed
        try
        {
            writeOutMappings(out);
        } catch (JAXBException ex)
        {
            Logger.getLogger(HardwareMappingPanel.class.getName()).
                    log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_saveMappingButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JComboBox networkCombo;
    private javax.swing.JLabel networkLabel;
    private javax.swing.JTextArea networkText;
    private javax.swing.JButton saveMappingButton;
    private javax.swing.JComboBox videoCombo;
    private javax.swing.JLabel videoLabel;
    private javax.swing.JTextArea videoText;
    // End of variables declaration//GEN-END:variables
}
